package com.ifeedme.rocketremote
{
	import flash.events.EventDispatcher;	
	import flash.events.Event;
	
	/**
	 * super class for reading/writing application preferences, eg frontend, keymap and other records
	 * intended to be supplimented by a sub class having interface IPrefsObject. Is there any way to actually *enforce* this in as3?
	 * @author Jim Hayes (www.ifeedme.com) may 2007
	 */	
	public class PrefsObject extends EventDispatcher
	{
		protected var _frontends:XML;
		protected var _keyMapXml:XML;
		
		function PrefsObject():void
		{
			// so far, nothing thats not handled by a subclasss
		}
		
		public final function get keyMapXml():XML
		{
			return _keyMapXml;
		}
		
		public final function get frontends():XML
		{
			return _frontends;
		}
		
		/**
		 *  raise an event to tell the main application that the prefs object has read all it's information and is ready
		 */
		protected final function signalReady():void
		{
			var readyEvent:Event = new Event("prefsReady");
			this.dispatchEvent(readyEvent);
		}
		
		
		/**
		 * create a new default, empty frontend record to be populated later.
		 * message the gui to inform it of this event
		 */		
		protected final function firstRun():void
        {
        	// note to self, this was painful, could have constructed the fragment as xml, then toString()ed it and added the comments, parent tags :(
        	// maybe ... formating the tabs would have just as bad ...
        	var defaultFrontends:String = "<frontends>";            	
        	defaultFrontends += "\n\t";
        	defaultFrontends += "<!-- example format";
        	defaultFrontends += "\n\t";
        	defaultFrontends += "<frontend default=\"true\">";
        	defaultFrontends += "\n\t\t";
        	defaultFrontends += "<nickname>Living room</nickname>";
        	defaultFrontends += "\n\t\t";
        	defaultFrontends += "<server>192.168.0.20</server>";
        	defaultFrontends += "\n\t\t";
        	defaultFrontends += "<port>6546</port>";
        	defaultFrontends += "\n\t";
        	defaultFrontends += "</frontend>";
        	defaultFrontends += "\n\t";
        	defaultFrontends += "-->";
        	defaultFrontends += "\n";
        	defaultFrontends += "</frontends>"; 
        	
        	//write an empty (default) prefs record - the subclass will handle this ...
        	   	
            writePrefs(defaultFrontends);
            
            //instanciate _frontends with above xml
            
            _frontends = new XML(defaultFrontends);
            
            // raise event to tell gui to go straight to the preferences page ....
            var firstRunEvent:Event = new Event("firstRun");
			this.dispatchEvent(firstRunEvent);
            //showPrefs();
            	
        }
        
        /**
         *  The following methods should be overridden by the sub class, 
         *  if there is an elegant method to handle this in as3 then I'm currently unaware of it :( 
         */
                 
        public function readPrefs():void
		{
			// should be overridden by sub class
		}
		
		public function writePrefs(str:String):void
		{
			// should be overridden by sub class
		}	
		
		protected function readFrontends():void
		{
			// should be overridden by sub class
		}
		
		protected function readKeyMap():void
		{
			// should be overridden by sub class
		}
		
		public function writeKeyMap(keyMapXml:XML):void
		{
			// should be overridden by sub class
		}
		
	}
}